/*
 * public.c
 *
 *  Created on: 2015-6-5
 *      Author: Pangxiao
 */

#include "Timerapp.h"

//==============PORT=================
const struct STIMER_MATE_SIGNAL stimer_mate_signal[] = {
  {_TimeRun_SysStart_, _TimeRun_SysEnd_ - _TimeRun_SysStart_, _TimeSig_SysTick_},
  {_TimeRun_Sys100MsStart_, _TimeRun_Sys100MsEnd_ - _TimeRun_Sys100MsStart_,
   _TimeSig_100msEvent_},
  {_TimeRun_Sys500MsStart_, _TimeRun_Sys500MsEnd_ - _TimeRun_Sys500MsStart_,
   _TimeSig_HalfSecEvent_},
  {_TimeRun_RtcHalfSecStart_, _TimeRun_RtcHalfSecEnd_ - _TimeRun_RtcHalfSecStart_,
   _TimeSig_100ms_},
  {_TimeRun_RtcSecStart_, _TimeRun_RtcSecEnd_ - _TimeRun_RtcSecStart_, _TimeSig_100ms_},  
};
struct STTIMERLIST *stTimer =  ( struct STTIMERLIST *)	_stTimerDatap;
unsigned short iTimerRunData[_TimeRun_End_];	

//==============PORT=================

void
Timer_Data_Refresh (unsigned short *iTimer, unsigned short ilen, unsigned short *iTimerSet)
{
  struct STTIMERLIST *stp = (struct STTIMERLIST *) iTimer;
  unsigned short i;

  for (i = 0; i < ilen; i++, stp++)
    {
      if (stp->b1Condition)
	{
	  if (0 == stp->iTm)
	    {
	      if (0 == stp->b1StateIo)
		stp->iTm = *(iTimerSet + i);
	    }
	  else
	    {
	      if (stp->iTm)
				{
		stp->iTm--;
					if ( 0 == stp->iTm)
						stp->b1StateIo = 1;
				}	      
	    }
	}
      else
	{
	  stp->b1StateIo = 0;
	  stp->iTm = 0;
	}
    }
}

void
Timer_UnitInit (unsigned short *iTimer, unsigned char cConditon,unsigned short *iTimerSet)
{
  struct STTIMERLIST *stp = (struct STTIMERLIST *)iTimer;
		
	stp->b1Condition = (cConditon)?1:0;
	Timer_Data_Refresh (iTimer,1,iTimerSet);  
}

void
Timer_Handle (unsigned short iTimeData,	unsigned short *iTimer, unsigned short *iTimerSet, 	struct STIMER_MATE_SIGNAL *stp  ,unsigned short iStDataLen)
{
  unsigned short i,j;
  for (i = 0;  i < iStDataLen; i++,stp++)
    {
      if (0 == (iTimeData & (1 << stp->cTimeSegnal)))
	continue;
			j = stp->iTimerReg ;
      Timer_Data_Refresh (iTimer + stp->iTimerReg,  stp->iTimerLen,
			  iTimerSet + j);
    }
}

void
Timer_Systick_Fun (void)
{
		struct STIMER_MATE_SIGNAL *stp = (struct STIMER_MATE_SIGNAL *)stimer_mate_signal;
	unsigned short iStDataLen = sizeof (stimer_mate_signal) / sizeof (stimer_mate_signal[0]);
	Timer_Handle( sttickint.uniontimesignal.iTimeListData,_stTimerDatap,_stTimerSetDatap,
		stp,iStDataLen);
}

void Timer_State_Push(char cGet,char cUpdate)
{
		struct STIMER_MATE_SIGNAL *stp = (struct STIMER_MATE_SIGNAL *)stimer_mate_signal;
	unsigned short iStDataLen = sizeof (stimer_mate_signal) / sizeof (stimer_mate_signal[0]);
	unsigned short i,j;
	unsigned short *iTimer =_stTimerDatap;
 
	struct STTIMERLIST *sTimer ;
	
  for (i = 0;  i < iStDataLen; i++,stp++)
    {   			
			for ( j  = stp->iTimerReg ; j<( stp->iTimerReg + stp->iTimerLen);j++ )
			{
					sTimer = (struct STTIMERLIST *)iTimer + j;		
				if ( cUpdate)
					sTimer->b1StateOld = sTimer->b1State ;				
				if ( cGet)
					sTimer->b1State = sTimer->b1StateIo ;
			}       
    }
}

void
Timer_Data_CheckUnit (unsigned short *iTimer, unsigned char cConditon, unsigned short *iTimerSet)
{
	 struct STTIMERLIST *stp = (struct STTIMERLIST *) iTimer;
  unsigned short i;
stp->b1Condition = (cConditon)?1:0;
  for (i = 0; i < 1; i++, stp++)
    {
      if (stp->b1Condition)
	{
	  if (0 == stp->iTm)
	    {
	      if (0 == stp->b1StateIo)
		stp->iTm = *(iTimerSet + i);
	    }
	  else
	    {
	      if (stp->iTm)
				{		 
				}	      
	    }
	}
      else
	{
	  stp->b1State = stp->b1StateIo = 0;
	  stp->iTm = 0;
	}
    }
}